﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CDStore3Tiers;
using CuaHangDiaNhac.Xuly;

namespace CuaHangDiaNhac
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string name = "";
        public frmPhatSinh fPhatSinh = null;
        public frmTaiKhoan fTaiKhoan = null;
        public frmSuKien fSukien = null;
        public frmDiaNhac fDiaNhac = null;
        public frmNhaCungCap fNCC = null;
        public frmDatHang fDH = null;
        public frmNhapHang fnh = null;
        public frmBaiHat fbh = null;
        public frmChiTietHoaDon fHoaDon = null;
        public frmQuanLyHoaDon qlHoaDon = null;
        public frmDanhSachBaiHat_DiaNhac frmDSBH = null;
        private frmDSGiamGia gg = null;
        public frmBackupRestore fbackup = null;
        InHoaDon frmInhoadon = null;


        public void lockLogin(bool val, int pq)
        {
            if (pq == 1)
            {

                btnLogout.Enabled = val;
                btnDiaNhac.Enabled = val;
                btnBaiHat.Enabled = val;
                bttDatHang.Enabled = val;
                btnNH.Enabled = val;
                bttNCC.Enabled = val;
                btnGiamGia.Enabled = val;
                btnTaiKhoan.Enabled = val;
                btnSuKien.Enabled = val;
                btnHoaDon.Enabled = val;
                btnThongKe.Enabled = val;
                lblWelcome.Visible = val;
                btnDoiMK.Enabled = val;
                btn_Backup.Enabled = val;
                btnLogin.Enabled = !val;
            }
            else if (pq == 2)
            {
                btnLogout.Enabled = val;
                btnDiaNhac.Enabled = val;
                btnBaiHat.Enabled = val;
                bttDatHang.Enabled = val;
                btnNH.Enabled = val;
                bttNCC.Enabled = val;
                btnGiamGia.Enabled = val;
                btnTaiKhoan.Enabled = !val;
                btnSuKien.Enabled = !val;
                btnHoaDon.Enabled = val;
                btnThongKe.Enabled = !val;
                lblWelcome.Visible = val;
                btnDoiMK.Enabled = val;
                btnLogin.Enabled = !val;
                btn_Backup.Enabled = !val;

            }
            else
            {
                btnLogout.Enabled = val;
                btnDiaNhac.Enabled = val;
                btnBaiHat.Enabled = val;
                bttDatHang.Enabled = !val;
                btnNH.Enabled = !val;
                bttNCC.Enabled = !val;
                btnGiamGia.Enabled = val;
                btnTaiKhoan.Enabled = !val;
                btnSuKien.Enabled = !val;
                btnHoaDon.Enabled = !val;
                btnThongKe.Enabled = !val;
                lblWelcome.Visible = val;
                btnDoiMK.Enabled = val;
                btnLogin.Enabled = !val;
                btn_Backup.Enabled = !val;
            }

        }



        public frmMain()
        {
            Xuly.XuLyTienTrinh.Start();
            InitializeComponent();
            rbTabThongTin.Focus();
        }

        private void btnDiaNhac_Click(object sender, EventArgs e)
        {
            if (fDiaNhac == null || fDiaNhac.IsDisposed)
            {
                fDiaNhac = new frmDiaNhac();
                fDiaNhac.MdiParent = frmMain.ActiveForm;
                fDiaNhac.WindowState = FormWindowState.Maximized;
                fDiaNhac.Show();
            }
            else
                fDiaNhac.Activate();
        }
        public void btnChiTietHoaDon_Click(object sender, EventArgs e)
        {
            if (fHoaDon == null || fHoaDon.IsDisposed)
            {
                fHoaDon = new frmChiTietHoaDon((int)sender);
                fHoaDon.MdiParent = frmMain.ActiveForm;
                fHoaDon.WindowState = FormWindowState.Maximized;
                fHoaDon.Show();
            }
            else
                fHoaDon.Activate();
        }

        private void btnHoaDon_Click(object sender, EventArgs e)
        {
            if (qlHoaDon == null || qlHoaDon.IsDisposed)
            {
                qlHoaDon = new frmQuanLyHoaDon();
                qlHoaDon.MdiParent = frmMain.ActiveForm;
                qlHoaDon.WindowState = FormWindowState.Maximized;
                qlHoaDon.Show();
            }
            else
                qlHoaDon.Activate();
        }

        public void btnChiTiet_Click(object sender, EventArgs e)
        {
            if (frmDSBH == null || frmDSBH.IsDisposed)
            {
                frmDSBH = new frmDanhSachBaiHat_DiaNhac((int)sender);
                frmDSBH.MdiParent = frmMain.ActiveForm;
                frmDSBH.WindowState = FormWindowState.Maximized;
                frmDSBH.Show();
            }
            else

                frmDSBH.Activate();
            frmDSBH.Getdata((int)sender);


        }
        public void btnIn_Click(object sender, EventArgs e)
        {
            if (frmInhoadon == null || frmInhoadon.IsDisposed)
            {
                frmInhoadon = new InHoaDon((int)sender);
                frmInhoadon.MdiParent = frmMain.ActiveForm;
                frmInhoadon.WindowState = FormWindowState.Maximized;
                frmInhoadon.Show();
            }
        }
        private void btnTaiKhoan_Click(object sender, EventArgs e)
        {
            if (fTaiKhoan == null || fTaiKhoan.IsDisposed)
            {
                fTaiKhoan = new frmTaiKhoan();
                fTaiKhoan.MdiParent = frmMain.ActiveForm;
                fTaiKhoan.WindowState = FormWindowState.Maximized;
                fTaiKhoan.Show();
            }
            else
                fTaiKhoan.Activate();
        }

        public void btnSuKien_Click(object sender, EventArgs e)
        {
            if (fSukien == null || fSukien.IsDisposed)
            {
                fSukien = new frmSuKien();
                fSukien.MdiParent = frmMain.ActiveForm;
                fSukien.WindowState = FormWindowState.Maximized;
                fSukien.Show();
            }
            else
                fSukien.Activate();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            frmLogin fr = new frmLogin();
            fr.ShowDialog();
            if (frmLogin.isLogin)
            {
                labelItem1.Text = "Chào mừng bạn " + frmLogin.ten + " đã đăng nhập vào phần mềm..................";
                lockLogin(true, frmLogin.qh);
            }
            int qh = frmLogin.qh;
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            DialogResult accept = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc muốn đăng xuất tài khoản ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            frmLogin fr = new frmLogin();
            fr.logout();
            if (accept == DialogResult.Yes)
            {
                lockLogin(false, 1);
                Form[] childArray = this.MdiChildren;
                foreach (Form childform in childArray)
                {
                    childform.Close();
                }
                labelItem1.Text = "Bạn chưa đăng nhập. Mời bạn nhấn vào nút Đăng nhập....................";
            }
        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            rbTabThongTin.Focus();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            rbTabThongTin.Focus();
            lockLogin(false, 1);

            //try
            //{
            //    lockLogin(false);
            //    managerCDDataContext cn = new managerCDDataContext();
            //    cn.Connection.ConnectionString = getConfig.getConnection();


            //    if (cn.USERs.Count() > 0) { }
            //}
            //catch
            //{
            //    frmConfig fr = new frmConfig();
            //    fr.ShowDialog();
            //}
            Xuly.XuLyTienTrinh.Stop();
        }

        private void bttNCC_Click(object sender, EventArgs e)
        {
            if (fNCC == null || fNCC.IsDisposed)
            {
                fNCC = new frmNhaCungCap();
                fNCC.MdiParent = frmMain.ActiveForm;
                fNCC.WindowState = FormWindowState.Maximized;
                fNCC.Show();
            }
            else
                fNCC.Activate();
        }

        public void bttDatHang_Click(object sender, EventArgs e)
        {
            if (fDH == null || fDH.IsDisposed)
            {
                fDH = new frmDatHang();
                fDH.MdiParent = frmMain.ActiveForm;
                fDH.WindowState = FormWindowState.Maximized;
                fDH.Show();
            }
            else
                fDH.Activate();
        }

        private void btnNH_Click(object sender, EventArgs e)
        {
            if (fnh == null || fnh.IsDisposed)
            {
                fnh = new frmNhapHang();
                fnh.MdiParent = frmMain.ActiveForm;
                fnh.WindowState = FormWindowState.Maximized;
                fnh.Show();
            }
            else
                fnh.Activate();
        }

        private void btnBaiHat_Click(object sender, EventArgs e)
        {
            if (fbh == null || fbh.IsDisposed)
            {
                fbh = new frmBaiHat();
                fbh.MdiParent = frmMain.ActiveForm;
                fbh.WindowState = FormWindowState.Maximized;
                fbh.Show();
            }
            else
                fbh.Activate();
        }
        public void btnMua_Click(object sender, EventArgs e)
        {
            if (fHoaDon == null || fHoaDon.IsDisposed)
            {
                fHoaDon = new frmChiTietHoaDon((int)sender);
                fHoaDon.MdiParent = frmMain.ActiveForm;
                fHoaDon.WindowState = FormWindowState.Maximized;
                fHoaDon.Show();
            }
            else
                fHoaDon.Activate();

            //if (qlHoaDon == null || qlHoaDon.IsDisposed)
            //{
            //    qlHoaDon = new frmQuanLyHoaDon();
            //    qlHoaDon.MdiParent = frmMain.ActiveForm;
            //    qlHoaDon.WindowState = FormWindowState.Maximized;
            //    qlHoaDon.Show();
            //}
            //else
            //    qlHoaDon.Activate();
        }

        public void bttThemDH_Click(object sender, EventArgs e)
        {
            if (qlHoaDon == null || qlHoaDon.IsDisposed)
            {
                qlHoaDon = new frmQuanLyHoaDon();
                qlHoaDon.MdiParent = frmMain.ActiveForm;
                qlHoaDon.WindowState = FormWindowState.Maximized;
                qlHoaDon.Show();
            }
            else
                qlHoaDon.Activate();
        }

        private void btnGiamGia_Click(object sender, EventArgs e)
        {
            XLSuKien sk = new XLSuKien();
            DateTime day = DateTime.Parse(System.DateTime.Now.ToShortDateString());
            int mask = sk.KiemTraThoiGian(day);
            if (mask != 0)
            {
                if (gg == null || gg.IsDisposed)
                {
                    gg = new frmDSGiamGia(mask);
                    gg.MdiParent = frmMain.ActiveForm;
                    gg.WindowState = FormWindowState.Maximized;
                    gg.Show();
                }
                else
                {
                    gg.Activate();
                }
            }
            else
            {
                gg = new frmDSGiamGia(mask);
            }
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            frmThongKe tk = new frmThongKe();
            tk.StartPosition = FormStartPosition.CenterParent;
            tk.ShowDialog();
        }

        private void btnDoiMK_Click(object sender, EventArgs e)
        {
            frmDoiMK doimk = new frmDoiMK();
            doimk.StartPosition = FormStartPosition.CenterParent;
            doimk.ShowDialog();
        }

        private void btn_Backup_Click(object sender, EventArgs e)
        {
            if (fbackup == null || fbackup.IsDisposed)
            {
                fbackup = new frmBackupRestore();
                fbackup.MdiParent = frmMain.ActiveForm;
                fbackup.WindowState = FormWindowState.Maximized;
                fbackup.Show();
            }
            else
                fbackup.Activate();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (fPhatSinh == null || fPhatSinh.IsDisposed)
            {
                fPhatSinh = new frmPhatSinh();
                fPhatSinh.MdiParent = frmMain.ActiveForm;
                fPhatSinh.WindowState = FormWindowState.Maximized;
                fPhatSinh.Show();
            }
            else
                fPhatSinh.Activate();
        }
    }
}